package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.admin.exception.BusinessException;
import com.tanhua.api.MovementApi;
import com.tanhua.api.UserInfoApi;
import com.tanhua.api.VideoApi;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.model.vo.VideoVo;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.http.client.UserTokenHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class ManagerService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private VideoApi videoApi;

    @DubboReference
    private MovementApi movementApi;

    @Autowired
    private RedisTemplate redisTemplate;

    //查询用户列表
    public PageResult findAllUsers(Integer page, Integer pagesize) {
        //1 调用api实现分页查询
        Page<UserInfo> pages = userInfoApi.findByPage(page,pagesize);

        List<UserInfo> list = pages.getRecords();

        for (UserInfo userInfo : list) {
            if (redisTemplate.hasKey(Constants.FREEZE_USER+userInfo.getId())){
                userInfo.setUserStatus("2");
            }
        }

        //2 封装分页对象，返回数据
        return new PageResult(page,pagesize, (int) pages.getTotal(),pages.getRecords());
    }


    /**
     * 根据id查询用户详情
     */
    public UserInfo findUserById(Long userId) {
        UserInfo userInfo = userInfoApi.findByid(userId);

        if (redisTemplate.hasKey(Constants.FREEZE_USER+userInfo.getId())){
            userInfo.setUserStatus("2");
        }

        return userInfo;
    }

    /**
     * 查询指定用户发布的所有视频列表
     */
    public PageResult findAllVideos(Integer page, Integer pagesize, Long uid) {
          PageResult pages =   videoApi.findVideosByUid(page,pagesize,uid);
          if (CollUtil.isEmpty(pages.getItems())){
              return new PageResult(page,pagesize,0,null);
          }
//          查询用户的详情
        UserInfo userInfo = userInfoApi.findByid(uid);
//          封装vos
        List<VideoVo> vos = new ArrayList();
        List<Video> items = pages.getItems();
        for (Video video : items) {
            VideoVo vo = VideoVo.init(userInfo, video);
            vos.add(vo);
        }
//        封装分页对象 返回结果
        return  new  PageResult(page,pagesize,pages.getCounts(),vos);
    }
    //查询动态
    public PageResult findAllMovements(Integer page, Integer pagesize, Long uid, Integer state) {
        //1 根据用户id和审核状态  分页查询动态数据
            PageResult pageResult = movementApi.findMovementsByUid(page,pagesize,uid,state);
            if (CollUtil.isEmpty(pageResult.getItems())){
                return new PageResult(page, pagesize,0,null);
            }
        UserInfo userInfo = userInfoApi.findByid(uid);

        List<MovementsVo> vos = new ArrayList();

        List<Movement> movements = pageResult.getItems();

        for (Movement movement : movements) {
            vos.add(MovementsVo.init(userInfo,movement));
        }
        return new PageResult(page, pagesize,pageResult.getCounts(),vos);
    }

//    冻结
    public Map freeze(Map params) {

        String userId = params.get("userId").toString();
        String freezingTime = params.get("freezingTime").toString();


        //2 获取冻结的失效时间    单位是天   冻结时间，1为冻结3天，2为冻结7天，3为永久冻结
        int day = 0;
        if (freezingTime.equals("1")){
            day =3;
        }
        if (freezingTime.equals("2")){
            day=7;
        }
        if (freezingTime.equals("3")){
            day=-1;
        }

        String value = JSON.toJSONString(params);

        redisTemplate.opsForValue().set(Constants.FREEZE_USER+userId,value, Duration.ofDays(day));

        Map map = new HashMap();
        map.put("massage","冻结成功!");
        return map;
    }
//用户解冻
    public Map userUnfreeze(Map params) {

        if (params.get("userId")==null){
            throw new BusinessException("参数不能为空!");
        }
        String userId = params.get("userId").toString();

        redisTemplate.delete(Constants.FREEZE_USER+userId);
        Map map = new HashMap();

        map.put("massage","解冻成功!");

        return map;
    }

}
